package com.itheima2.dao;

import com.itheima2.domain.Student;
import com.itheima2.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

public class StudentDaoImpl implements StudentDao {
    @Override
    public ArrayList<Student> findAll() {
        //创建集合保存student对象 并返回
        ArrayList<Student> list = new ArrayList<>();
        Connection con = null;
        Statement stat = null;
        ResultSet rs = null;
        //异常获取
        try {
// 获取con连接
            con = JDBCUtils.getConnection();

            //获取执行者对象
            stat = con.createStatement();

            //执行sql语句
            String sql = "SELECT * FROM student";
            rs = stat.executeQuery(sql);

            //处理结果集
            while (rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");

                //封装学生对象
                Student stu = new Student(sid, name, age, birthday);
                list.add(stu);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,stat,rs);
        }
        return list;

    }

    @Override
    public Student findById(Integer id) {
        return null;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int update(Student stu) {
        return 0;
    }

    @Override
    public int insert(Student stu) {
        return 0;
    }


}
